﻿/// <reference path="../libs/_references.js" />

window.vmFactory = (function () {
    var data = null;

    //function rentTheBook (event) {
    //    var $target = $(event.target);
    //    var bookId = $.data($target, 'bookId')
    //    data.books.rentBook(bookId)
    //        .then(function () {
    //            if (successCallback) {
    //                successCallback();
    //            }
    //        })
    //}

	function getLoginViewModel(successCallback) {		
		var viewModel = {
			username: "DonchoMinkov",
			password: "123456q",
			login: function () {
				data.users.login(this.get("username"), this.get("password"))
					.then(function () {
						if (successCallback) {
							successCallback();
						}
					});
			},
			register: function () {
				data.users.register(this.get("username"), this.get("password"))
					.then(function () {
						if (successCallback) {
							successCallback();
						}
					});
			}
		};
		return kendo.observable(viewModel);
	};

	function getBooksViewModel() {
		return data.books.all()
			.then(function (books) {
			    console.log(books);
				var viewModel = {
				    books: books,
				    rentBook: function (event) {
				        var $target = $(event.target);
				        var bookId = $target.attr("data-bookId");
				        data.books.rentBook(parseInt(bookId));
				    },
					message: ""
				};
				return kendo.observable(viewModel);
			});
	}

	function getBooksFreeViewModel(successCallback) {
	    return data.books.free()
			.then(function (books) {
			    var viewModel = {
			        books: books,
			        rentBook: function (event) {
			            var $target = $(event.target);
			            var bookId = $target.attr("data-bookId");
			            data.books.rentBook(parseInt(bookId))
                            .then(function () {
                                if (successCallback) {
                                    successCallback();
                                }
                            });
			        },
			        message: ""
			    };
			    return kendo.observable(viewModel);
			});
	}

	//function getBooksAuthorViewModel(author) {
	//    return data.books.byAuthor(author)
	//		.then(function (books) {
	//		    var viewModel = {
	//		        books: books,
	//		        message: ""
	//		    };
	//		    return kendo.observable(viewModel);
	//		});
	//}

	function getBooksGenreViewModel(genre, successCallback) {
	    return data.books.byGenre(genre)
			.then(function (books) {
			    var viewModel = {
			        books: books,
			        rentBook: function (event) {
			            var $target = $(event.target);
			            var bookId = $target.attr("data-bookId");
			            data.books.rentBook(parseInt(bookId))
                            .then(function () {
                                if (successCallback) {
                                    successCallback();
                                }
                            });
			        },
			        message: ""
			    };
			    return kendo.observable(viewModel);
			});
	}

	function getStoresViewModel() {
	    return data.stores.all()
			.then(function (stores) {
			    console.log(stores);
			    var viewModel = {
			        stores: stores,
			        message: ""
			    };
			    return kendo.observable(viewModel);
			});
	}

	function getStoreFullViewModel(id, successCallback) {
	    return data.stores.byId(id)
			.then(function (store) {
			    var viewModel = {
			        store: store,
			        rentBook: function (event) {
			            var $target = $(event.target);
			            var bookId = $target.attr("data-bookId");
			            data.books.rentBook(parseInt(bookId))
                            .then(function () {
                                if (successCallback) {
                                    successCallback();
                                }
                            });
			        },
			        message: ""
			    };
			    return kendo.observable(viewModel);
			});
	}

	function getUserViewModel(successCallback) {
	    return data.users.profile()
			.then(function (user) {
			    //console.log(store);
			    var viewModel = {
			        user: user,
			        returnBook: function (event) {
			            var $target = $(event.target);
			            var bookId = $target.attr("data-bookId");
			            data.books.rentBook(parseInt(bookId))
			                .then(function () {
			                    if (successCallback) {
			                        successCallback();
			                    }
			                });
			        },
			        message: ""
			    };
			    return kendo.observable(viewModel);
			});
	}

	return {
		getLoginVM: getLoginViewModel,
		getBooksVM: getBooksViewModel,
		getBooksFreeVM: getBooksFreeViewModel,
	    //getBooksAuthorVM: getBooksAuthorViewModel,
		getUserVM: getUserViewModel,
		getBooksGenreVM: getBooksGenreViewModel,
		getStoresVM: getStoresViewModel,
		getStoreFullVM: getStoreFullViewModel,
		setPersister: function (persister) {
			data = persister
		}
	};
}());